<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li a {
            float: right;
        }
    </style>
</head>

<body>
    <div>
        <textarea name="" id="" cols="30" rows="10"></textarea>
        <button>发布</button>
        <ul></ul>
    </div>
    <script>
        var text = document.querySelector('textarea');
        var btn = document.querySelector('button');
        var ul = document.querySelector('ul');
        var li = document.createElement('li');
        btn.onclick = function () {
            if (text.value === '') {
                alert('请输入留言');
                return false;
            } else
                var li = document.createElement('li'); //创造节点要放在onclick点击事件里面，不能放点击事件外面
            li.innerHTML = text.value + "<a href ='javascript:;'>删除</a>";
            ul.insertBefore(li, ul.children[0]); //将li节点添加至ul父节点，并且放在父节点的第一个孩子前面

            var as = document.querySelectorAll('a');
            for (var i = 0; i < as.length; i++) {
                as[i].onclick = function () {
                    ul.removeChild(this.parentNode);//移除a标签的父节点
                }
            }
        }
    </script>
</body>

</html>